package pl.un4giv3n.mightvsmagic.admin.action;

import javax.persistence.EntityManager;

import pl.un4giv3n.mightvsmagic.action.PersistenceAction;
import pl.un4giv3n.mightvsmagic.entity.BonusType;
import pl.un4giv3n.mightvsmagic.entity.Guardian;
import pl.un4giv3n.mightvsmagic.entity.User;
import pl.un4giv3n.mightvsmagic.entity.UserType;

/**
 * @author andrzejewskia
 * this action adds new items to the bazar
 * this action should be performed by admin ONLY
 */
public class EditGuardianPageAction extends PersistenceAction {

	private static final long serialVersionUID = 1L;
	
	private Long guardianId;
	
	/**
	 * session user
	 */
	private User user;

	private Guardian guardian;

	/* (non-Javadoc)
	 * @see pl.un4giv3n.mightvsmagic.action.PersistenceAction#execute()
	 */
	@Override
	public String execute() {
		EntityManager entityManager = getEntityManager();
		user = getSessionUser(entityManager);
		if (user == null || user.getUserType() != UserType.ADMIN) {
			addActionError("Zaloguj się");
			entityManager.close();
			return LOGIN;
		}
		if(guardianId != null){
			guardian = entityManager.find(Guardian.class, guardianId);
		}
		return SUCCESS;
	}

	public Long getGuardianId() {
		return guardianId;
	}

	public void setGuardianId(Long guardianId) {
		this.guardianId = guardianId;
	}

	public Long getId(){
		return guardian == null ? null : guardian.getId();
	}
	
	public String getName(){
		return guardian == null ? null : guardian.getName();
	}
	public String getDesc(){
		return guardian == null ? null : guardian.getDescription();
	}
	
	public BonusType getBonus(){
		return guardian == null ? null : guardian.getBonus();
	}
}
